import {Database} from '@/types/api/database'
import {logoMap} from '@/utils/logoLoader'
import {Encoding, LogicalDecodingOutputPlugin, ReadIsolation, SslMode} from '@/enums/connectionFormConst'
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 默认支持的数据连接类型表单属性配置 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
import type {FormItemRule} from 'element-plus'

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 默认支持的数据连接类型常量数据 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
export const Databases: Database[] = [
  { type: 'mysql', name: 'MySQL', logo: logoMap['mysql'], disabled: false },
  { type: 'postgresql', name: 'PostgreSQL', logo: logoMap['postgresql'], disabled: false },
  { type: 'sqlserver', name: '微软 SQL Server', logo: logoMap['sqlserver'], disabled: false },
  { type: 'greenplum', name: 'Greenplum', logo: logoMap['greenplum'], disabled: false },
  { type: 'doris', name: 'Apache Doris', logo: logoMap['doris'], disabled: true },
  { type: 'oracle', name: 'Oracle', logo: logoMap['oracle'], disabled: true },
  { type: 'db2', name: 'DB2', logo: logoMap['db2'], disabled: true },
  { type: 'clickhouse', name: 'ClickHouse', logo: logoMap['clickhouse'], disabled: true },
  { type: 'gaussdb', name: 'GaussDB', logo: logoMap['gaussdb'], disabled: true },
  { type: 'oceanbase', name: 'OceanBase', logo: logoMap['oceanbase'], disabled: true },
]

export const SearchingDatabases: Database[] = [
  { type: 'elastic_search', name: 'Elastic Search SQL', logo: logoMap['elastic_search'], disabled: true },
  { type: 'solr', name: 'Apache Solr', logo: logoMap['solr'], disabled: true },
]

export  const HadoopDatabases: Database[] = [
  { type: 'hive', name: 'Hive', logo: logoMap['hive'], disabled: true },
  { type: 'spark', name: 'Spark SQL', logo: logoMap['spark'], disabled: true },
  { type: 'hbase', name: 'HBase', logo: logoMap['hbase'], disabled: true },
  { type: 'argodb', name: 'ArgoDB', logo: logoMap['argodb'], disabled: true },
]

export  const CloudDatabases: Database[] = [
  { type: 'analyticDBMySQL', name: 'AnalyticDB MySQL', logo: logoMap['analyticdbmysql'], disabled: true },
  { type: 'maxcompute', name: 'Maxcompute', logo: logoMap['maxcompute'], disabled: true },
  { type: 'maxcomputepostgre', name: 'Maxcompute PostgreSQL', logo: logoMap['maxcomputepostgre'], disabled: true },
]

export const NosqlDatabases: Database[] = [
  { type: 'mongodb', name: 'MongoDB BI Connector', logo: logoMap['mongodb'], disabled: true },
]

export const AllDatabases = [...Databases, ...SearchingDatabases, ...HadoopDatabases, ...CloudDatabases, ...NosqlDatabases]

export const DbTypeMap: Record<string, Database> = Object.fromEntries(AllDatabases.map(db => [db.type, db]))


// 定义基础表单字段接口
interface BaseFormField {
  label: string;
  prop: string;
  type: 'input' | 'password' | 'number' | 'textarea' | 'select' | 'switch';
  required?: boolean;
  placeholder?: string;
  options?: { label: string; value: any }[];
  hidden?: boolean;
  rules?: FormItemRule[];
}

// 定义共享的验证规则
const commonRules: Record<string, FormItemRule[]> = {
  title: [
    { required: true, message: '请输入连接名称', trigger: 'blur' },
    { min: 2, max: 50, message: '长度在 2 到 50 个字符', trigger: 'blur' }
  ],
  host: [
    { required: true, message: '请输入机器地址', trigger: 'blur' }
  ],
  port: [
    { required: true, message: '请输入端口号', trigger: 'blur' },
    { type: 'number', message: '端口必须为数字值', trigger: 'blur' }
  ],
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' }
  ],
  maxConnNum: [
    { type: 'number', message: '最大连接数必须为数字值', trigger: 'blur' }
  ],
}

// 定义数据库表单配置接口
interface DatabaseFormConfig {
  basicFields: BaseFormField[];
  authFields: BaseFormField[];
  advancedFields: BaseFormField[];
  defaultValues: Record<string, any>;
}

// 数据库表单配置映射
export const databaseForms: Record<string, DatabaseFormConfig> = {
  mysql: {
    basicFields: [
      { label: '连接名称', prop: 'title', type: 'input', required: true, rules: commonRules.title },
      { label: '类型', prop: 'options.type', type: 'input', hidden: true },
      { label: '机器地址', prop: 'options.host', type: 'input', required: true, rules: commonRules.host },
      { label: '端口', prop: 'options.port', type: 'number', required: true, rules: commonRules.port },
      { label: '数据库', prop: 'options.database', type: 'input' },
    ],
    authFields: [
      { label: '用户名', prop: 'options.username', type: 'input', required: true, rules: commonRules.username },
      { label: '密码', prop: 'options.password', type: 'password', required: true, rules: commonRules.password },
    ],
    advancedFields: [
      { label: '最大连接数', prop: 'options.maxConnNum', type: 'number', required: true, rules: commonRules.maxConnNum },
      { label: '编码', prop: 'options.encoding', type: 'select', options: [...Encoding] },
      { label: '读事务隔离级别', prop: 'options.readIsolation', type: 'select', options: [...ReadIsolation] },
      { label: '只显示指定数据库/模式下的表', prop: 'options.onlyShowSpecifiedSchema', type: 'switch' },
    ],
    defaultValues: {
      title: '本地_MySQL',
      options: {
        type: 'mysql',
        host: 'localhost',
        port: 3306,
        database: 'pocket_boot',
        username: 'pocketBoot',
        password: 'PocketBoot@123',
        maxConnNum: 5,
        encoding: 'utf8',
        readIsolation: 'READ_UNCOMMITTED',
        onlyShowSpecifiedSchema: false
      }
    }
  },

  postgresql: {
    basicFields: [
      { label: '类型', prop: 'options.type', type: 'input', hidden: true },
      { label: '连接名称', prop: 'title', type: 'input', required: true, rules: commonRules.title },
      { label: '机器地址', prop: 'options.host', type: 'input', required: true, rules: commonRules.host },
      { label: '端口', prop: 'options.port', type: 'number', required: true, rules: commonRules.port },
      { label: '数据库', prop: 'options.database', type: 'input' },
      { label: '模式', prop: 'options.schema', type: 'input', placeholder: '模式（Schema）' },
    ],
    authFields: [
      { label: '用户名', prop: 'options.username', type: 'input', required: true, rules: commonRules.username },
      { label: '密码', prop: 'options.password', type: 'password', required: true, rules: commonRules.password },
    ],
    advancedFields: [
      { label: '最大连接数', prop: 'options.maxConnNum', type: 'number', required: true, rules: commonRules.maxConnNum },
      { label: '编码', prop: 'options.encoding', type: 'select', options: [...Encoding] },
      { label: 'SSL模式', prop: 'sslMode', type: 'select', options: [...SslMode] },
      { label: 'LDOP', prop: 'sslMode', type: 'select', options: [...LogicalDecodingOutputPlugin], placeholder: 'Logical Decoding Output Plugin' },
      { label: '读事务隔离级别', prop: 'options.readIsolation', type: 'select', options: [...ReadIsolation] },
      { label: '只显示指定数据库/模式下的表', prop: 'options.onlyShowSpecifiedSchema', type: 'switch' },
    ],
    defaultValues: {
      title: '本地_PostgresSQL',
      options: {
        type: 'postgresql',
        host: 'localhost',
        port: 5432,
        database: 'pocket',
        schema: '',
        username: 'pocket',
        password: 'pocket@123',
        encoding: 'utf8',
        maxConnNum: 10,
        sslMode: false,
        readIsolation: 'READ_UNCOMMITTED',
        onlyShowSpecifiedSchema: false
      }
    }
  },

  sqlserver: {
    basicFields: [
      { label: '类型', prop: 'options.type', type: 'input', hidden: true },
      { label: '连接名称', prop: 'title', type: 'input', required: true, rules: commonRules.title },
      { label: '机器地址', prop: 'options.host', type: 'input', required: true, rules: commonRules.host },
      { label: '端口', prop: 'options.port', type: 'number', required: true, rules: commonRules.port },
      { label: '数据库', prop: 'options.database', type: 'input' },
    ],
    authFields: [
      { label: '用户名', prop: 'options.username', type: 'input', required: true, rules: commonRules.username },
      { label: '密码', prop: 'options.password', type: 'password', required: true, rules: commonRules.password },
    ],
    advancedFields: [
      { label: '最大连接数', prop: 'options.maxConnNum', type: 'number', required: true, rules: commonRules.maxConnNum },
      { label: '读事务隔离级别', prop: 'options.readIsolation', type: 'select', options: [...ReadIsolation] },
      { label: '只显示指定数据库/模式下的表', prop: 'options.onlyShowSpecifiedSchema', type: 'switch' },
    ],
    defaultValues: {
      title: 'Job_SqlServer',
      options: {
        type: 'sqlserver',
        host: '',
        port: 1433,
        database: '',
        username: '',
        password: '',
        maxConnNum: 5,
        readIsolation: 'READ_UNCOMMITTED',
        onlyShowSpecifiedSchema: false
      }
    }
  },

  greenplum: {
    basicFields: [
      { label: '类型', prop: 'options.type', type: 'input', hidden: true },
      { label: '连接名称', prop: 'title', type: 'input', required: true, rules: commonRules.title },
      { label: '机器地址', prop: 'options.host', type: 'input', required: true, rules: commonRules.host },
      { label: '端口', prop: 'options.port', type: 'number', required: true, rules: commonRules.port },
      { label: '数据库', prop: 'options.database', type: 'input' },
      { label: '模式', prop: 'options.schema', type: 'input', placeholder: '模式（Schema）' },
    ],
    authFields: [
      { label: '用户名', prop: 'options.username', type: 'input', required: true, rules: commonRules.username },
      { label: '密码', prop: 'options.password', type: 'password', required: true, rules: commonRules.password },
    ],
    advancedFields: [
      { label: '最大连接数', prop: 'options.maxConnNum', type: 'number', required: true, rules: commonRules.maxConnNum },
      { label: '编码', prop: 'options.encoding', type: 'select', options: [...Encoding] },
      { label: '只显示指定数据库/模式下的表', prop: 'options.onlyShowSpecifiedSchema', type: 'switch' },
    ],
    defaultValues: {
      title: '内网_GreenPlum',
      options: {
        type: 'greenplum',
        host: 'localhost',
        port: 5432,
        database: 'pocket',
        schema: '',
        username: 'pocket',
        password: 'pocket@123',
        encoding: 'utf8',
        maxConnNum: 5,
        onlyShowSpecifiedSchema: false
      }
    }
  },

  mongodb: {
    basicFields: [
      { label: '类型', prop: 'options.type', type: 'input', hidden: true },
      { label: '连接名称', prop: 'title', type: 'input', required: true, rules: commonRules.title },
      { label: '机器地址', prop: 'options.host', type: 'input', required: true, rules: commonRules.host },
      { label: '端口', prop: 'options.port', type: 'number', required: true, rules: [...commonRules.port, { max: 65535, message: '端口号不能超过65535', trigger: 'blur' }] },
      { label: '数据库', prop: 'options.database', type: 'input' },
    ],
    authFields: [
      { label: '用户名', prop: 'options.username', type: 'input', rules: [] },
      { label: '密码', prop: 'options.password', type: 'password', rules: [] },
    ],
    advancedFields: [
      { label: '数据库名称', prop: 'database', type: 'input' },
      { label: '认证机制', prop: 'authMechanism', type: 'select', options: [
        { label: 'SCRAM-SHA-1', value: 'SCRAM-SHA-1' },
        { label: 'SCRAM-SHA-256', value: 'SCRAM-SHA-256' },
      ]},
      { label: '连接参数', prop: 'options.params', type: 'textarea' },
    ],
    defaultValues: {
      title: '新建连接1',
      options: {
        type: 'mongodb',
        host: 'localhost',
        port: 27017,
        username: 'pocketBoot',
        password: 'PocketBoot@123',
        encoding: 'utf8'
      }
    }
  },
} 